home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
tex
/
cawf404.zip
/
cawf.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-07
|
16KB
|
648 lines
/*
* cawf - a C version of Henry Spencer's awf(1), the Amazingly
* Workable (text) Formatter
*
* V. Abell, Purdue University Computing Center
*/
/*
* Copyright (c) 1991 Purdue University Research Foundation,
* West Lafayette, Indiana 47907. All rights reserved.
*
* Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
* University Computing Center. Not derived from licensed software;
* derived from awf(1) by Henry Spencer of the University of Toronto.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The author is not responsible for any consequences of use of
* this software, even if they arise from flaws in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission. Credits must appear in the
* documentation.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software. Credits must
* appear in the documentation.
*
* 4. This notice may not be removed or altered.
*/
static char Version[] = "4.04";
#include "cawf.h"
#include <time.h>
#include <sys/stat.h>
#ifndef UNIX
#include <io.h>
#include <process.h>
#include <string.h>
#include <sys\types.h>
#include <sys\stat.h>
#endif
static char *Month[] =
{ "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
main(argc, argv)
int argc;
char *argv[];
{
char dt[32]; /* date for td and DY strings */
char *ep; /* environment pointer */
int fff = 0; /* final form feed status */
char *files[MAXFILES]; /* file names */
int help = 0; /* help status */
int i; /* temporary index */
size_t l; /* length */
char *lib = CAWFLIB; /* library path */
int libl; /* library path length */
struct tm *lt; /* local time structure pointer */
int mac = 0; /* macro specification status */
int nf = 0; /* number of files */
char *np; /* name pointer */
int pc; /* prolog count */
struct stat sbuf; /* stat buffer */
time_t tm; /* system time of day */
/*
* Save program name.
*/
if ((Pname = strrchr(argv[0], '\\')) != NULL)
Pname++;
else if ((Pname = strrchr(argv[0], '/')) != NULL)
Pname++;
else
Pname = argv[0];
/*
* Set error file stream pointer.
*/
Efs = stderr;
/*
* Get library name.
*/
if ((np = getenv("CAWFLIB")) != NULL)
lib = np;
libl = strlen(lib);
/*
* Get device file name.
*/
for (ep = getenv("TERM");; ep = NULL) {
if (ep == NULL || *ep == '\0')
ep = "dumb";
l = libl + 1 + strlen(ep) + strlen(".dev") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE,
" no string space for device file: ", ep);
(void) sprintf(np, "%s/%s.dev", lib, ep);
if (stat(np, &sbuf) == 0)
break;
if (strcmp(ep, "dumb") == 0)
Error(FATAL, NOLINE, " no dumb.dev file in ", lib);
(void) free(np);
}
files[nf++] = np;
/*
* Get common text file name.
*/
l = libl + 1 + strlen("common") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE, " no string space for common file name",
NULL);
(void) sprintf(np, "%s/common", lib);
files[nf++] = np;
/*
* Process options.
*/
while ((i = getopt(argc, argv, "c:d:ef:hm:n:o:")) != EOF) {
switch (i) {
/*
* -c c
*
* Set the device configuration file path to c.
*/
case 'c':
Devconf = optarg;
break;
/*
* -d d
*
* Set the output device name to d.
*
* The default output device name is NORMAL -- i.e., a device that
* does bold face with backspace and overprinting and italic face with
* underscore. NORMAL is usually a terminal device.
*
* There is a built-in device, named ANSI, that does bold face with
* the ANSI shadow mode and italic face with the ANSI underscore mode.
* ANSI is normally a terminal device that supports the ANSI shadow
* and underscore modes.
*
* There is a built-in output device, named NONE, that does nothing
* at all for the bold or italic faces. This is usually a terminal
* device.
*
* All other device names must match a stanza in the device
* configuration file.
*/
case 'd':
Device = optarg;
break;
/*
* -e -- eject: issue final form feed
*/
case 'e':
fff = 1;
break;
/*
* -f f
*
* Set f as the font name for the output device (from the device
* configuration file).
*/
case 'f':
Devfont = optarg;
break;
/*
* -h -- display help (usage)
*/
case 'h':
help = 1;
break;
/*
* -m m
*
* Set the macro file name to m.
*
* Special support is provided for -man, -me and -ms.
*/
case 'm':
if (mac) {
Error(WARN, NOLINE,
"multiple macro file declaration",
NULL);
break;
}
l = libl + 2 + strlen(optarg) + strlen(".mac") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE, " no string space for ",
argv[1]);
(void) sprintf(np, "%s/m%s.mac", lib, optarg);
files[nf++] = np;
if (strcmp(optarg, "an") == 0)
Marg = MANMACROS;
else if (strcmp(optarg, "e") == 0)
Marg = MEMACROS;
else if (strcmp(optarg, "s") == 0)
Marg = MSMACROS;
mac++;
break;
/*
* -n n
*
* Set the starting page number to n.
*/
case 'n':
Thispg = atoi(optarg);
break;
/*
* -o o
*
* Set the pages to print list from the string o.
*
* Entries are separated by commas. A range is specified as n-m.
* A starting range of the form -m has one for a starting value,
* and a range of the form n- has an ending value equivalent to
* the end of the document. For example:
*
* -5,8,10-
*
* specifies the printing of pages 1 through 5, 8, and 10 through
* the end of the document.
*/
case 'o':
if (!AsmPgRange(optarg))
Error(WARN, NOLINE, "invalid page list: ",
optarg);
break;
/*
* Option not recognized by getopt().
*/
case '?':
Err = 1;
}
}
if (Defdev())
Err++;
if (help || Err) {
(void) fprintf(stderr,
"%s %s usage: [-c c] [-d d] [-e] [-f f] [-h] [-m m]\n",
Pname, Version);
(void) fprintf(stderr, "\t[-n n] [-o o] file...\n");
(void) fprintf(stderr,
"\t-c c c is the device configuration file path\n");
(void) fprintf(stderr,
"\t (default = %s/%s)\n", CAWFLIB, DEVCONFIG);
(void) fprintf(stderr,
"\t-d d d is the output device name\n");
(void) fprintf(stderr,
"\t (default = NORMAL, using \\b for bold and italic)\n");
(void) fprintf(stderr,
"\t (built-ins = ANSI, NONE and NORMAL)\n");
(void) fprintf(stderr,
"\t-e issue eject after last page\n");
(void) fprintf(stderr,
"\t-f f f is the output device font name\n");
(void) fprintf(stderr,
"\t-h display help (this output)\n");
(void) fprintf(stderr,
"\t-m m m is the macro file name\n");
(void) fprintf(stderr,
"\t-n n n is the starting page number\n");
(void) fprintf(stderr,
"\t-o o o specifies output pages and page ranges\n");
(void) fprintf(stderr,
"\tfile ... source file names\n");
exit(Err);
}
if (mac == 0) {
/*
* No macros - enable Bold, Italic and Roman fonts.
*/
for (i = 0; Fcode[i].nm; i++) {
switch (Fcode[i].nm) {
case 'B':
case 'I':
case 'R':
Fcode[i].status = '1';
}
}
}
/*
* Add user-supplied file names.
*/
pc = nf;
if (optind >= argc) {
files[nf++] = NULL; /* STDIN */
} else {
while (optind < argc) {
if (nf >= MAXFILES)
Error(WARN, NOLINE, " too many files at ",
argv[optind]);
files[nf++] = argv[optind++];
}
}
/*
* Make sure all input files are accessible.
*/
for (i = 0; i < nf; i++) {